﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Customer.Popup
{
    public partial class AddTag : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["UserTagID"]))
                {
                    UserTagDAL tag = new UserTagDAL();
                    UserTagInfo tagInfo = tag.GetUserTagByUserTagID(Convert.ToInt32(Request["UserTagID"]));

                    if (tagInfo != null)
                    {
                        tb_Alias.Text = tagInfo.Alias;
                        tb_TagUID.Text = tagInfo.TagUID;
                        tb_ServiceURL.Text = tagInfo.TagContent;
                        dl_TagType.SelectedValue = tagInfo.TagType;

                    }
                    if (Request["IsRemove"] == "true")
                    {
                        tb_Alias.ReadOnly = true;
                        tb_ServiceURL.ReadOnly = true;
                        tb_TagUID.ReadOnly = true;
                        dl_TagType.Enabled = false;
                        btn_Submit.Visible = false;
                        btn_Remove.Visible = true;
                        lb_Remove.Visible = true;

                    }
                }
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {

            UserTagDAL tag = new UserTagDAL();
            UserTagInfo tagInfo = new UserTagInfo();
            if (!string.IsNullOrEmpty(Request["UserTagID"]))
            {
                tagInfo = tag.GetUserTagByUserTagID(Convert.ToInt32(Request["UserTagID"]));

                if (tagInfo != null)
                {
                    tagInfo.TagUID = tb_TagUID.Text.Trim();
                    tagInfo.Alias = tb_Alias.Text.Trim();
                    tagInfo.TagType = dl_TagType.SelectedValue.Trim();
                    if (tb_ServiceURL.Text.Contains("http"))
                        tagInfo.TagContent = tb_ServiceURL.Text.Trim();
                    else
                        tagInfo.TagContent = "http://" + tb_ServiceURL.Text.Trim();
                    tag.UpdateUserTag(tagInfo);
                }


            }
            else
            {
                tagInfo.TagUID = tb_TagUID.Text.Trim();
                tagInfo.TagType = dl_TagType.SelectedValue.Trim();
                tagInfo.Alias = tb_Alias.Text.Trim();
                tagInfo.UserGUID = WebUtility.UserGUID;
                if (tb_ServiceURL.Text.Contains("http"))
                    tagInfo.TagContent = tb_ServiceURL.Text.Trim();
                else
                    tagInfo.TagContent = "http://" + tb_ServiceURL.Text.Trim();
                tagInfo.Void = 0;
                int tagid = tag.AddUserTag(tagInfo);
            }

            ClientScript.RegisterStartupScript(this.GetType(), "update", "<script>parent.document.location.href=parent.document.location.href;parent.document.location.reload();</script>");
        }

        protected void btn_Remove_Click(object sender, EventArgs e)
        {
            UserTagDAL tag = new UserTagDAL();
            if (!string.IsNullOrEmpty(Request["UserTagID"]))
            {
                tag.DeleteUserTag(Convert.ToInt32(Request["UserTagID"]));
            }
            else
            {
                return;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "remove", "<script>parent.document.location.href=parent.document.location.href;parent.document.location.reload();</script>");

        }
    }
}